package com.micro.service.auth.server.redis;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author lvxiucai
 * @description 现该配置仅连接老系统用户远程模块单机redis
 * @date 2019/11/26
 */
@Configuration
@Slf4j
public class RedisTemplateConfig {

    //newSys
    @Value("${spring.redis.newSys.host}")
    private String newSysHost;
    @Value("${spring.redis.newSys.port}")
    private String newSysPort;
    @Value("${spring.redis.newSys.password}")
    private String newSysPassword;
    @Value("${spring.redis.jedis.pool.max-idle}")
    private Integer NEW_MAX_IDLE;
    @Value("${spring.redis.jedis.pool.max-active}")
    private Integer NEW_MAX_TOTAL;

    //建立连接最长等待时间
    private static final long MAX_WAIT_MILLIS = 10000;

    @Bean
    LettuceConnectionFactory lettuceConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(0);
        redisStandaloneConfiguration.setHostName(newSysHost);
        redisStandaloneConfiguration.setPort(Integer.parseInt(newSysPort));
        redisStandaloneConfiguration.setPassword(RedisPassword.of(newSysPassword));

        LettuceClientConfiguration.LettuceClientConfigurationBuilder lettuceClientConfigurationBuilder = LettuceClientConfiguration.builder();
        LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration,
                lettuceClientConfigurationBuilder.build());
        log.info("LettuceConnectionFactory实例化完成==========="+factory);
        return factory;
    }


    //配置工厂
    public RedisConnectionFactory connectionFactory(String host, int port, String password, int maxIdle,
                                                    int maxTotal, long maxWaitMillis, int index) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(host);
        jedisConnectionFactory.setPort(port);

        if (!StringUtils.isEmpty(password)) {
            jedisConnectionFactory.setPassword(password);
        }

        if (index != 0) {
            jedisConnectionFactory.setDatabase(index);
        }

        jedisConnectionFactory.setPoolConfig(poolConfig(maxIdle, maxTotal, maxWaitMillis, false));
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    //连接池配置
    public JedisPoolConfig poolConfig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxWaitMillis(maxWaitMillis);
        poolConfig.setTestOnBorrow(testOnBorrow);
        return poolConfig;
    }

    /**
     *StringTempldate--newSys
     */
    @Bean(name = "redisNewSysTemplate")
    public StringRedisTemplate newSysTemplate() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(
                connectionFactory(newSysHost, Integer.parseInt(newSysPort), newSysPassword, NEW_MAX_IDLE, NEW_MAX_TOTAL, MAX_WAIT_MILLIS, 0));
        return template;
    }



}
